package cn.yhsx.jenkins.plugins.rotator;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.EnvironmentContributor;
import hudson.model.Run;
import hudson.model.TaskListener;

import javax.annotation.Nonnull;

/**
 * insert into the build environment ：{@link RotatorWrapper#CURRENT_DATA}、{@link RotatorWrapper#LAST_DATA}、rotator parameters
 */
@Extension
public class RotatorEnvironmentContributor extends EnvironmentContributor {

    @Override
    public void buildEnvironmentFor(@Nonnull Run run, @Nonnull EnvVars envs,
                                    @Nonnull TaskListener listener) {

        RotatorAction action = run.getAction(RotatorAction.class);

        if (action != null) {
            envs.put(RotatorWrapper.CURRENT_DATA, action.getCurrentData());
            if (action.getLast() != null) {
                envs.put(RotatorWrapper.LAST_DATA, action.getLast().getCurrentData());
            }
            envs.putAll(action.getCurrentParameters());
        }
    }
}